"""
请求日志 Request VO
"""

import utils.enum_utils as enum
from models import (
    ListRequestBase,
    QueryFieldBase,
    RequestBase,
)
from my_typing import *
from utils.data_utils.special import ObjectIdStr
from utils.validator_utils.pydantic_validator.field import (
    PathCharField,
    QueryCharField,
    QueryIntegerField,
)


class RequestLogPathField:
    """ 请求日志 | 路径 | 字段 """

    request_log_id: ObjectIdStr = PathCharField(..., title="请求日志 ID")


class RequestLogDetailRequest(RequestBase):
    """ 请求日志 | 详情 | 请求 """

    request_log_id: ObjectIdStr = RequestLogPathField.request_log_id


class RequestLogListRequest(ListRequestBase):
    """ 请求日志 | 列表 | 请求 """

    # -------------------- 排序字段 --------------------
    sort_by_method: enum.MongoSortOrNone = QueryCharField(None, title="根据请求方法排序")
    sort_by_request_time: enum.MongoSortOrNone = QueryCharField(None, title="根据日志访问时间排序")
    sort_by_user_id: enum.MongoSortOrNone = QueryCharField(None, title="根据用户 ID 排序")

    # -------------------- 查询字段 --------------------
    method: enum.Method | None = QueryCharField(None, title="请求方法")
    object_id: ObjectIdStr | None = QueryCharField(None, title="对象 ID")
    path: StrOrNone = QueryCharField(None, title="请求路径")
    request_id: StrOrNone = QueryCharField(None, title="请求 ID")
    request_time_end: DateTimeOrNone = QueryCharField(None, title="日志访问结束时间")
    request_time_start: DateTimeOrNone = QueryCharField(None, title="日志访问开始时间")
    user_id: ObjectIdStr | None = QueryCharField(None, title="用户 ID")
    use_time_max: FloatOrNone = QueryIntegerField(None, title="使用时间（秒）小于等于")
    use_time_min: FloatOrNone = QueryIntegerField(None, title="使用时间（秒）大于等于")

    @classmethod
    def from_query(
        cls,
        page: int = QueryFieldBase.page,
        page_size: int = QueryFieldBase.page_size,
        sort_by_method: enum.MongoSortOrNone = QueryCharField(None, title="根据请求方法排序"),
        sort_by_request_time: enum.MongoSortOrNone = QueryCharField(None, title="根据日志访问时间排序"),
        sort_by_user_id: enum.MongoSortOrNone = QueryCharField(None, title="根据用户 ID 排序"),
        method: enum.Method | None = QueryCharField(None, title="请求方法"),
        object_id: ObjectIdStr | None = QueryCharField(None, title="对象 ID"),
        path: StrOrNone = QueryCharField(None, title="请求路径"),
        request_id: StrOrNone = QueryCharField(None, title="请求 ID"),
        request_time_end: DateTimeOrNone = QueryCharField(None, title="日志访问结束时间"),
        request_time_start: DateTimeOrNone = QueryCharField(None, title="日志访问开始时间"),
        user_id: ObjectIdStr | None = QueryCharField(None, title="用户 ID"),
        use_time_max: FloatOrNone = QueryIntegerField(None, title="使用时间（秒）小于等于"),
        use_time_min: FloatOrNone = QueryIntegerField(None, title="使用时间（秒）大于等于"),
    ) -> "RequestLogListRequest":
        return cls(
            page=page,
            page_size=page_size,
            sort_by_method=sort_by_method,
            sort_by_request_time=sort_by_request_time,
            sort_by_user_id=sort_by_user_id,
            method=method,
            object_id=object_id,
            path=path,
            request_id=request_id,
            request_time_end=request_time_end,
            request_time_start=request_time_start,
            user_id=user_id,
            use_time_max=use_time_max,
            use_time_min=use_time_min,
        )
